package vip.web3.api.service.impl.biz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import vip.web3.api.dao.biz.RecordDao;
import vip.web3.api.dao.biz.UserDao;
import vip.web3.api.entity.biz.RecordEntity;
import vip.web3.api.entity.biz.UserEntity;
import vip.web3.api.form.user.RechargeForm;
import vip.web3.api.form.user.RecordForm;
import vip.web3.api.mapper.RecordMapper;
import vip.web3.api.service.biz.OrderService;
import vip.web3.api.service.biz.RecordService;
import vip.web3.api.vo.biz.RecordVo;
import vip.web3.common.enums.OperateEnum;
import vip.web3.common.utils.DateUtils;
import vip.web3.common.utils.PageUtils;
import vip.web3.common.utils.Servlets;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RecordServiceImpl extends ServiceImpl<RecordDao, RecordEntity> implements RecordService {

    @Autowired
    private OrderService orderService;

    @Resource
    private UserDao userDao;

    @Override
    public PageUtils records(RecordForm form, Integer userId) {
        int type = form.getType();
        if(type == 1){
            // 购买记录
            return orderService.buyRecords(form, userId);
        }
        form.setBeginIndex((form.getPage() - 1) * form.getLimit());
        QueryWrapper<RecordEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_date");
        queryWrapper.eq("user_id", userId);
        if (type == 2) {
            // 充值记录
            queryWrapper.and(wrapper -> wrapper.eq("type", 1));
        } else if (type == 3) {
            // 提现记录
            queryWrapper.and(wrapper -> wrapper.eq("type", 2));
        } else if (type == 4) {
            // 流水记录
            queryWrapper.and(wrapper -> wrapper.in("type", 3,5,6,7,8,9));
        }
        Integer count = baseMapper.selectCount(queryWrapper);
        queryWrapper.last("limit " + form.getBeginIndex() + "," + form.getLimit());
        List<RecordEntity> entities = baseMapper.selectList(queryWrapper);

        List<RecordVo> assetsRecordVos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(entities)) {
            for (RecordEntity entity : entities) {
                RecordVo vo = new RecordVo();
                vo.setType(entity.getType());
                vo.setTypeName(entity.getTypeName());
                vo.setRemark(entity.getRemark());
                vo.setHash(entity.getHash());
                vo.setFee(entity.getFee().setScale(2, RoundingMode.DOWN).stripTrailingZeros().toPlainString());;
                vo.setAmount(entity.getAmount().setScale(2, RoundingMode.DOWN).stripTrailingZeros().toPlainString());
                vo.setOperateType(entity.getOperateType());
                vo.setStatus(entity.getStatus());
                vo.setTime(DateUtils.format(entity.getCreateDate(), DateUtils.DATE_TIME_PATTERN));
                assetsRecordVos.add(vo);
            }
        }
        return new PageUtils(assetsRecordVos, count, form.getLimit(), form.getPage());
    }

    @Override
    public void recharge(RechargeForm form) {
        // TODO 链上充值接口
        UserEntity user = this.userDao.selectById(Servlets.getUserId());
        RecordEntity entity = new RecordEntity();
        entity.setUserId(user.getId());
        entity.setMobile(user.getMobile());
        entity.setType(OperateEnum.TYPE1.getType());
        entity.setTypeName(OperateEnum.TYPE1.getMark());
        entity.setRemark(OperateEnum.TYPE1.getDesc());
        entity.setAmount(form.getAmount());
        entity.setFee(BigDecimal.ZERO);
        entity.setOperateType(form.getType());
        entity.setStatus(0);
        entity.setAddress(user.getBscAddress());
        entity.setCreateDate(new Date());
        entity.setUpdateDate(new Date());
        baseMapper.insert(entity);
    }
}
